package com.sport.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sport.po.User;
import com.sport.service.UserService;

@Controller
public class UserAction {
	@Autowired
	private UserService userService;
	
	
	public void delete(int id){
		
	}
	
	@RequestMapping("/UpdateUser.action")
	public String  update(@Validated User user,BindingResult bingingResult,Model model,HttpSession session,HttpServletRequest request) throws UnsupportedEncodingException{
		String error=null;
		if(bingingResult.hasErrors()){
			List<ObjectError> allErrors=bingingResult.getAllErrors();
			for (ObjectError objectError : allErrors) {
				System.out.println(objectError.getDefaultMessage());
				error=new String(objectError.getDefaultMessage().getBytes("ISO-8859-1"),"UTF-8");
			
				System.out.println("error:"+error);
			}
		}
		
		if(error==null){
		System.out.println(user);
		User user1=(User)session.getAttribute("user");
		user.setId(user1.getId());
		user.setCreate_time(user1.getCreate_time());
		user.setPassword(user1.getPassword());
		userService.update(user);
		session.setAttribute("user", user);
		}else{
			if(error.equals("真实姓名不能为空")){
				session.setAttribute("errorRealName",error);
			}else if(error.equals("超出了字符长度")){
				session.setAttribute("errorUserName",error);
			}
		}
		return "center/centerUpdate";
	}
	
	public void selectById(int id,HttpSession session){
		User user=userService.selectById(id);
		session.setAttribute("user", user);
		
	}
	
	
}
